module.exports=function (done) {
	$.router.post('/api/topic/add',$.checkLogin,async function (req,res,next) {
		req.body.author=req.session.user._id;
		if ('tags' in req.body) {
			req.body.tags=req.body.tags.split(',').map(v=>v.trim()).filter(v=>v);
		}
		const topic=await $.method('topic.add').call(req.body);
		res.success(topic);
	});

	$.router.get('/api/topic/list',async function (req,res,next) {
		if ('tags' in req.query) {
			req.query.tags=req.query.tags.split(',').map(v =>v.trim()).filter(v=>v);
		}
		const list=await $.method('topic.list').call(req.query);
		res.success(list);
	});

	$.router.get('/api/topic/:topic_id',async function (req,res,next) {
		const topic=await $.method('topic.get').call({_id: req.params.topic_id});
		if (!topic) {
			return next(new Error(`topic ${req.params.topic_id} does not exist`));
		}
		res.success({topic});
	});

	$.router.put('/api/topic/:topic_id',$.checkTopicAuthor,async function (req,res,next) {
		req.body._id=req.params.topic_id;
		if ('tags' in req.body) {
			req.body.tags=req.body.tags.split(',').map(v =>v.trim()).filter(v=>v);
		}
		const result=await $.method('topic.update').call(req.body);
		res.success({result});
	});

	$.router.delete('/api/topic/:topic_id',$.checkTopicAuthor,async function (req,res,next) {
		const result=await $.method('topic.delete').call({_id: req.params.topic_id});
		res.success({result});
	});

	$.router.post('/api/topic/:topic_id/comment',async function (req,res,next) {
		let body=req.body;
		body._id=req.params.topic_id;
		//body.author=req.session.user._id;
		const result=await $.method('topic.comment.add').call(body);
		res.success({result});
	});
	$.router.delete('/api/topic/:topic_id/comment/:comment_id',async function (req,res,next) {
		let body=req.body;
		body._id=req.params.topic_id;
		body.comment_id=req.params.comment_id;
		//body.author=req.session.user._id;
		const result=await $.method('topic.comment.delete').call(body);
		res.success({result});
	});
	done();
}